package java8.atguigu.teacher.datatime;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;

/**
 * @program lambda
 * @description:
 * @author: zhanglu
 * @create: 2019-06-02 14:54:00
 */
public class TestNewDateAPI {

    /*
        LocalDate LocalTime LocalDateTime
     */
    @Test
    public void test01(){

        LocalDate ld = LocalDate.now();
        System.out.println(ld);

        LocalTime lt = LocalTime.now();
        System.out.println(lt);

        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);
        System.out.println(ldt.getYear());
        System.out.println(ldt.getMonth());
        System.out.println(ldt.getMonthValue());

        LocalDateTime ldt2 = LocalDateTime.of(2015,10,19,13,22,33);
        System.out.println(ldt2);

        LocalDateTime ldt3 = ldt.plusHours(2);
        System.out.println(ldt3);

        LocalDateTime ldt4 = ldt.minusHours(3);
        System.out.println(ldt4);
    }

    /*
        Instant : 时间戳（以Unix元年：1970年1月1日00:00:00 莫哥时间 之间的毫秒值）
     */
    @Test
    public void test02(){
        Instant instant = Instant.now();    //默认获取UTC 时区
        System.out.println(instant);
        System.out.println(instant.toEpochMilli());

        OffsetDateTime odt = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(odt);

        Instant instant1 = Instant.ofEpochSecond(60);
        System.out.println(instant1);
    }

    /*
        Duration: 计算两个时间之间的间隔
        Period: 计算两个日期之间的间隔
     */
    @Test
    public void test03() throws InterruptedException {

        //时间间隔
        Instant instant1 = Instant.now();

        Thread.sleep(1000);

        Instant instant2 = Instant.now();

        Duration duration = Duration.between(instant1,instant2);
        System.out.println(duration.toMillis());


        LocalTime lt1 = LocalTime.now();

        Thread.sleep(1005);

        LocalTime lt2 = LocalTime.now();

        System.out.println(Duration.between(lt1, lt2).toMillis());


        //日期间隔
        LocalDate ld1 = LocalDate.of(2015, 1, 1);
        LocalDate ld2 = LocalDate.now();
        Period period = Period.between(ld1, ld2);
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());
    }

    /*
        TemporalAdjuster: 时间校正器
     */
    @Test
    public void test04(){
        LocalDateTime ldt1 = LocalDateTime.now();
        System.out.println(ldt1);

        LocalDateTime ldt2 = ldt1.withDayOfMonth(5);
        System.out.println(ldt2);

        LocalDateTime ldt3 = ldt1.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
        System.out.println(ldt3);

        //自定义，下一个工作日
        LocalDateTime ldt5 = ldt1.with(l -> {
            LocalDateTime ldt4 = (LocalDateTime) l;
            DayOfWeek dow = ldt4.getDayOfWeek();
            if(dow.equals(DayOfWeek.FRIDAY)){
                return ldt4.plusDays(3);
            }else if(dow.equals(DayOfWeek.SATURDAY)){
                return ldt4.plusDays(2);
            }else {
                return ldt4.plusDays(1);
            }
        });
        System.out.println(ldt5);

    }

    /*
        DateTimeFormatter: 格式化时间/日期
     */
    @Test
    public void test06(){
        DateTimeFormatter dtf = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        LocalDateTime ldt = LocalDateTime.now();
        String s1 = ldt.format(dtf);
        System.out.println(s1);

        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String s2 = ldt.format(dtf2);
        System.out.println(s2);
        LocalDateTime ldt2 = LocalDateTime.parse(s2, dtf2);
        System.out.println(ldt2);
    }

    /*
        时区 ZonedDate ZonedTime zonedDateTime
     */
    @Test
    public void test07(){
        Set<String> set = ZoneId.getAvailableZoneIds();
//        set.forEach(System.out::println);

        LocalDateTime ldt1 = LocalDateTime.now(ZoneId.of("Europe/London"));
        System.out.println(ldt1);

        //带时区的
        LocalDateTime ldt2 = LocalDateTime.now(ZoneId.of("Europe/London"));
        ZonedDateTime zdt = ldt2.atZone(ZoneId.of("Europe/London"));
        System.out.println(zdt);
    }

}
